//
//  Int+Ex.swift
//  FastComponent
//
//  Created by lxf on 2022/10/27.
//

import Foundation


public extension Bool{
    
    func toUInt8()->UInt8{
        return self ? 1 :0
    }
    
}
public extension UInt8{
    // truncating
    func toInt()->Int{
        return Int(self)
    }
}
public extension UInt16{
    // truncating
    func toInt()->Int{
        return Int(self)
    }
    func toUInt8()->UInt8{
        let uint8self = UInt8.init(truncating: self as NSNumber)
        return uint8self
    }
}
public extension UInt32{
    // toUInt8()
    func toUInt8()->UInt8{
        let uint8self = UInt8.init(truncating: self as NSNumber)
        return uint8self
    }
}
public extension Int{
    // truncating
    func toUInt8()->UInt8{
        let uint8self = UInt8.init(truncating: self as NSNumber)
        return uint8self
    }
    func toUInt16()->UInt16{
        let v = UInt16.init(truncating: self as NSNumber)
        return v
    }
    // truncating
    func toInt8()->Int8{
        let int8self = Int8.init(truncating: self as NSNumber)
        return int8self
    }
}
public extension Array  where Element == Int{
    func toUInt8List()->[UInt8]{
        return map { v in
            v.toUInt8()
        }
    }
}
public extension Array  where Element == UInt8{
    
    func toHexList()->[String]{
        return map { e in
            Int(e).toHex()
        }
    }
    
    mutating func safe_replece(indexs:[Int],values:[Int]){
        guard indexs.count != 0,indexs.count  == values.count else {
            return
        }
        
        indexs.forEach { i, indexV in
            if i < 0 || i >= self.count{
                XXLoger.e("fastcomponent:  out of range!!!")
                return
            }
            
            self[indexV] = values[i].toUInt8()
        }
    }
    mutating func safe_replece(indexs:[String:Int],values:[String:UInt8]){
        guard indexs.count != 0,indexs.count  == values.count else {
            
            if values.count != indexs.count {
                XXLoger.e("参数/值,数量不相等");
                XXLoger.e(indexs);
                XXLoger.e(values);
            }
            
            return
        }
        
     
        
        indexs.forEach { (key: String, i: Int) in
            if let v = values[key]{
                if i < 0 || i >= self.count{
                    XXLoger.e("fastcomponent:  out of range!!!")
                    return
                }
                self[i] = v
            }else{
                XXLoger.e("fastcomponent:  参数未找到!!!" + key)
            }
        }
    }
}



public extension Array  where Element == String{
    func toUInt8List()->[UInt8]{
        return map { v in
            v.hexToUInt8() ?? 0
        }
    }
    func toUInt8List(_ errCallback:(Int,String)->(UInt8))->[UInt8]{
        
        var list = [UInt8].init()
        self.forEach { index, v in
            if let v_uint8  = v.hexToUInt8(){
                list.append(v_uint8)
            }else{
                let v = errCallback(index,v)
                list.append(v)
            }
        }
        
        return list
    }
}

public extension String{
    
    func toUInt8()->UInt8?{
        return self.toInt()?.toUInt8()
    }
    
    func hexToUInt8()->UInt8?{
        return self.hexToInt()?.toUInt8()
    }
    
}
